import { NestFactory } from '@nestjs/core';
import { ValidationPipe } from '@nestjs/common';
import { NestExpressApplication } from '@nestjs/platform-express'
// import { Request, Response, NextFunction } from 'express';
import * as cors from 'cors';
import { join } from 'path'
import { AppModule } from './app.module';
import { Response } from './common/response';
import { HttpFilter } from './common/exceptionsFilter';

//全局中间件
// function middleWareAll(req: Request, res: Response, next: NextFunction) {
//     next()
// }

async function bootstrap() {
    const app = await NestFactory.create<NestExpressApplication>(AppModule);

    // 跨域
    app.use(cors())

    // app.use(middleWareAll)

    // 接口传入属性验证，DTO属性必须添加验证装饰器，忽略未定义字段
    app.useGlobalPipes(new ValidationPipe({ whitelist: true }));

    // 响应拦截器
    app.useGlobalInterceptors(new Response())

    // 异常拦截器
    app.useGlobalFilters(new HttpFilter())

    // 静态目录访问
    app.useStaticAssets(join(__dirname, 'images'), {
        prefix: "/img"
    })

    await app.listen(4000);
}
bootstrap();